spolecznoscfandomcom-20200214-history
Moduł:Slider
local RandomSlider = {} RandomSlider.modulo = nil function RandomSlider.timeMod() return 86400 -- Co 1 dzień -- return 300 -- Co 5 minut end function RandomSlider.spread(frame) local slides = slides(frame.args) local tab = mw.html.create('table'):addClass('article-table sortable'):css('border', '1px solid #CCC') local counts = {100, 1000, 10000, 100000} local tr = tab:tag('tr') :tag('th') :wikitext('Nr') :done() :tag('th') :addClass('unsortable') :done() for _,v in ipairs(counts) do tr:tag('th'):wikitext('w ' .. v) end local res = {} for _,field in pairs(slides.fields) do local i = tostring(field.index) resi = {} for _,v in ipairs(counts) do resitostring(v) = 0 end end local seed = getSeed() for count = 1, counts#counts do local list = getFourSlides(slides, seed) local ids = {list1, list2, list3, list4} for _,i in ipairs(ids) do i = tostring(i) for _,j in ipairs(counts) do if count <= j then j = tostring(j) resij = resij + 1 end end end seed = seed + 1 end for _,field in pairs(slides.fields) do local i = tostring(field.index) local tr = tab:tag('tr'):css('text-align', 'right') tr :tag('td') :css('text-align', 'center') :wikitext(i) :done() :tag('td') :css('text-align', 'center') :css('padding', '1px') :wikitext(' ') :done() for _,v in ipairs(counts) do tr:tag('td'):wikitext(resitostring(v)) end end return tab end function RandomSlider.seeds(frame) local slides = slides(frame.args) local tab = mw.html.create('table'):addClass('article-table'):css('border', '1px solid #CCC') tab :tag('tr') :tag('th') :wikitext('Ziarno') :done() :tag('th') :wikitext('Slajd #1') :done() :tag('th') :wikitext('#2') :done() :tag('th') :wikitext('#3') :done() :tag('th') :wikitext('#4') :done() local first = true local seed = getSeed() local startSeed = seed - (seed % RandomSlider.modulo) local endSeed = startSeed - 1 + RandomSlider.modulo * 2 for i = startSeed, endSeed do local list = getFourSlides(slides, i) if i > startSeed and i % RandomSlider.modulo 0 then tab:tag('tr'):tag('th'):attr('colspan', '5'):css('text-align', 'center'):wikitext('Losowanie') end local timestamp = i*RandomSlider.timeMod() local tr = tab:tag('tr') :tag('td') :css('line-height', '100%') :wikitext(tostring(i) .. ' ' .. mw.getCurrentFrame():preprocess(' ') .. ' ') :done() if i seed then tr:addClass('accent') end for _,i in ipairs(list) do local field = slides.fieldsi tr :tag('td') :css('padding', '2px') :wikitext(' ') :done() end end return tab end function RandomSlider.grid(frame) local slides = slides(frame.args) local seed = tonumber(frame:getParent().args'seed') or getSeed() local list = getFourSlides(slides, seed) local lines = {} for _,i in ipairs(list) do local field = slides.fieldsi table.insert(lines, mw.ustring.format( ' %s %s ', field'Obraz', field'Tytuł', field'Link', field'Link', field'Tytuł', field'Opis' )) end return mw.getCurrentFrame():preprocess(' \n' .. table.concat(lines, ) .. ' ') end function RandomSlider.slider(frame) local slides = slides(frame.args) local seed = tonumber(frame:getParent().args'seed') or getSeed() local list = getFourSlides(slides, seed) local lines = {} for _,i in ipairs(list) do local field = slides.fieldsi table.insert(lines, mw.ustring.format( 'File:%s|%s|link=%s|linktext=%s', field'Obraz', field'Tytuł', field'Link', field'Opis' )) end return mw.getCurrentFrame():preprocess('\n' .. table.concat(lines, '\n') .. '\n') end function RandomSlider.list(frame) local slides = slides(frame.args, false) local tab = mw.html.create('table'):addClass('article-table'):css('width', '100%'):css('border', '1px solid #CCC') tab :tag('tr') :tag('th') :wikitext('Nr') :css('width', '25px') :done() :tag('th') :wikitext('Obraz') :done() :tag('th') :wikitext('Szczegóły') :attr('colspan', '2') :done() local count, count_invalid, count_inactive = 0, 0, 0 local first = true for _,i in ipairs(slides.indexes) do field = slides.fieldsi if first then first = false else tab :tag('tr') :tag('td') :attr('colspan', '4') :css('padding', '2px') end local tr1 = tab :tag('tr') :tag('td'):wikitext(tostring(i)) :attr('rowspan', '3') :css('text-align', 'center') :done() :tag('td'):attr('rowspan', '3') :css('padding', '1px') :addClass('cs-slider-image') :wikitext(' ') :css('width', '201px') :css('height', '108px') :css('border', '1px solid #CCC') :done() :tag('td'):wikitext('\'\'\'Tytuł:\'\'\) :css('width', '1px') :css('white-space', 'nowrap') :done() :tag('td'):wikitext(field'Tytuł') :done() local tr2 = tab :tag('tr') :tag('td'):wikitext('\'\'\'Opis:\'\'\'') :done() :tag('td'):wikitext(field'Opis') :done() local tr3 = tab :tag('tr') :tag('td'):wikitext('\'\'\'Link:\'\'\'') :done() :tag('td'):wikitext(field'Link') :done() if not isValid(field) then tr1:css('color', 'red'):attr('title', 'Błędny slajd - wymagane są wszystkie 4 parametry') tr2:css('color', 'red'):attr('title', 'Błędny slajd - wymagane są wszystkie 4 parametry') tr3:css('color', 'red'):attr('title', 'Błędny slajd - wymagane są wszystkie 4 parametry') count_invalid = count_invalid+1 elseif not isActive(field) then tr1:css('opacity', '.3'):attr('title', 'Slajd wyłączony w związku z brakiem aktywności') tr2:css('opacity', '.3'):attr('title', 'Slajd wyłączony w związku z brakiem aktywności') tr3:css('opacity', '.3'):attr('title', 'Slajd wyłączony w związku z brakiem aktywności') count_inactive = count_inactive+1 else count = count+1 end end local infotab = mw.html.create('table'):addClass('article-table'):css('border', '1px solid #CCC') infotab:tag('tr') :tag('td'):wikitext('\'\'\'Ilość poprawnych:\'\'\'') :done() :tag('td'):wikitext(count) :done() if count_invalid > 0 then infotab:tag('tr') :tag('td'):wikitext('\'\'\'Ilość niepoprawnych:\'\'\'') :done() :tag('td'):wikitext(count_invalid) :done() end if count_inactive > 0 then infotab:tag('tr') :tag('td'):wikitext('\'\'\'Ilość wyłączonych:\'\'\'') :done() :tag('td'):wikitext(count_inactive) :done() end infotab:tag('tr') :tag('td'):wikitext('\'\'\'Ilość zestawów:\'\'\'') :done() :tag('td'):wikitext(math.floor(count/4)) :done() local timeMod = RandomSlider.timeMod() local timeTD = infotab:tag('tr') :tag('td'):wikitext('\'\'\'Nowy slider co:\'\'\'') :done() :tag('td') local h,m,s = timeFromSec(RandomSlider.timeMod()) if h>0 then timeTD:wikitext(h .. 'h ') end if m>0 then timeTD:wikitext(m .. 'm ') end if s>0 then timeTD:wikitext(s .. 's ') end return infotab, tab end function timeFromSec(seconds) local hours, minutes = 0, 0 minutes = math.floor(seconds/60) hours = math.floor(minutes/60) seconds = seconds - minutes*60 minutes = minutes - hours*60 return hours, minutes, math.floor(seconds) end function getSeed() return math.floor(RandomSlider.now/RandomSlider.timeMod()) end function table.shuffle(tab) local n, order, res = #tab, {}, {} for i=1,n do orderi = { rnd = math.random(), idx = i } end table.sort(order, function(a,b) return a.rnd < b.rnd end) for i=1,n do resi = tab[orderi.idx] end return res end function table.split(tab) local n, res = #tab, {} local pivot = math.random(2,n) for i=pivot,n do table.insert(res, tabi) end for i=1,pivot-1 do table.insert(res, tabi) end return res end function table.contains(tab, element) for _, value in pairs(tab) do if value element then return true end end return false end function getFourSlides(slides, seed) seed = tonumber(seed) local indexes = randomizeSlides(slides, seed) local offset, list = (seed % RandomSlider.modulo)*4, {} for i=1+offset,4+offset do table.insert(list, indexesi) end return list end function randomizeSlides(slides, seed) if RandomSlider.lists nil then RandomSlider.lists = {} end seed = seed - (seed % RandomSlider.modulo) if RandomSlider.liststostring(seed) ~= nil then return RandomSlider.liststostring(seed) end math.randomseed(seed) local list = table.shuffle(table.split(table.shuffle(table.shuffle(slides.indexes)))) RandomSlider.liststostring(seed) = list return list end function isValid(field) return field'Link' ~= nil and field'Tytuł' ~= nil and field'Opis' ~= nil and field'Obraz' end function isActive(field) return field'Wyłączony' nil end function slides(args, filter) if filter nil then filter = true end local args = arguments(args) local fields = {} local indexes = {} for k, v in pairs(args) do local keySplit = mw.text.split(tostring(k) or '', ':') local index = tonumber(mw.text.trim(tostring(keySplit1))) local key = mw.text.trim(tostring(keySplit2)) local value = mw.text.trim(tostring(v)) if keySplit2 ~= nil then if type(index) 'number' and index > 0 then if fieldsindex nil then table.insert(indexes, index) fieldsindex = {index=index} end fieldsindexkey = value end else if not value then Skins.varsmw.text.trim(tostring(k)) = value end end end if filter then indexes = {} for k, v in pairs(fields) do if not isValid(fieldsk) or not isActive(fieldsk) then fieldsk = nil else table.insert(indexes, k) end end end table.sort(indexes) if RandomSlider.modulo nil then RandomSlider.modulo = math.floor(#indexes/4) else RandomSlider.modulo = math.min(math.floor(#indexes/4), RandomSlider.modulo) end RandomSlider.tz = tonumber(mw.getCurrentFrame():preprocess(' >= and > |7200|3600}}')) -- +2h w letnim, +1h w zimowym - na bazie: http://spolecznosc.wikia.com/wiki/Blog_u%C5%BCytkownika:SH3VEK/Snippety:_Czas RandomSlider.now = os.time(os.date('!*t')) + RandomSlider.tz return { fields = fields, indexes = indexes, } end function arguments(origArgs) local args = {} for k, v in pairs(origArgs) do if type(v) 'string' then v = mw.text.trim(v) end if v ~= '' then argsk = v end end return args end return RandomSlider --